﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CC.Yacht.Windows.Forms
{
    [DesignTimeVisible(false)]
    public partial class GameListItem : UserControl
    {
        #region Constructor
        public GameListItem()
        {
            InitializeComponent();
            HighlightColor = Color.LightGreen;
            NonHighlightColor = Color.White;
            UpdateBorders();
            UpdateControls();
        }
        #endregion

        #region Private Fields
        private BorderSides _BorderSides = BorderSides.Bottom;
        private Game _Game;
        private bool _HighlightForced;
        #endregion

        #region Public Properties
        [Category("Appearance")]
        public BorderSides BorderSides
        {
            get { return _BorderSides; }
            set
            {
                _BorderSides = value;
                UpdateBorders();
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Game Game
        {
            get { return _Game; }
            set
            {
                _Game = value;
                Name = (_Game != null ? _Game.GameId.ToString() : string.Empty);
                UpdateControls();
            }
        }

        [Category("Appearance")]
        public Color HighlightColor { get; set; }

        //[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        //public new string Name
        //{
        //    get { return (_Game != null ? _Game.GameId.ToString() : string.Empty); }
        //    set { }
        //}

        [Category("Appearance")]
        public Color NonHighlightColor { get; set; }
        #endregion

        #region Private Event Handlers
        private void CommonControl_MouseClick(object sender, MouseEventArgs e)
        {
            OnMouseClick(e);
        }

        private void CommonControl_MouseEnter(object sender, EventArgs e)
        {
            OnMouseEnter(e);
        }

        private void CommonControl_MouseLeave(object sender, EventArgs e)
        {
            OnMouseLeave(e);
        }
        #endregion

        #region Private Methods
        private void InternalHighlight(bool highlight, bool forceHighlight)
        {
            if (forceHighlight)
            {
                _HighlightForced = highlight;
                BackColor = (highlight ? HighlightColor : NonHighlightColor);
            }
            else if (!_HighlightForced)
            {
                BackColor = (highlight ? HighlightColor : NonHighlightColor);
            }
        }

        private void UpdateBorders()
        {
            _LabelBorderBottom.Visible = (_BorderSides & BorderSides.Bottom) == BorderSides.Bottom;
            _LabelBorderLeft.Visible = (_BorderSides & BorderSides.Left) == BorderSides.Left;
            _LabelBorderRight.Visible = (_BorderSides & BorderSides.Right) == BorderSides.Right;
            _LabelBorderTop.Visible = (_BorderSides & BorderSides.Top) == BorderSides.Top;            
        }

        private void UpdateControls()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(UpdateControls));
            }
            else
            {
                if (_Game != null)
                {
                    _LabelOwner.Text = _Game.Owner.Username;
                    _DieLabelPlayers.Face = (DieFace) _Game.Players.Count;
                    _DieLabelPlayers.Text = _Game.Players.Count + "/" + Constants.MaximumPlayers;
                    _LabelStatus.Text = _Game.IsStarted ? "(Started)" : "(Waiting)";
                }

                Application.DoEvents();
            }
        }
        #endregion

        #region Protected Methods
        protected override void OnMouseEnter(EventArgs e)
        {
            InternalHighlight(true, false);
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            InternalHighlight(false, false);
            base.OnMouseLeave(e);
        }
        #endregion

        #region Public Methods
        public void Highlight(bool highlight)
        {
            InternalHighlight(highlight, true);
        }
        #endregion
    }
}
